#pragma once 
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<string>
#include<string.h>
#include<arpa/inet.h>
#include <netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include"log.hpp"
#include<signal.h>
#include"Task.hpp"
#include"Threadpool.hpp"

using namespace std;

static const string localip="0.0.0.0";
  static const int gbacklog=5;

class Tcpserver
{public:
    Tcpserver(uint16_t port,string ip=localip):_ip(ip),_port(port),_listensockfd(-1)
    {}
    ~Tcpserver(){}
    void initserver()
    {    _listensockfd=socket(AF_INET,SOCK_STREAM,0);//不能进行声明
         if(_listensockfd<0)
         { 
            logmessage(ERROR,"socket error");
          //  cout<<"socket error"<<endl;
         }
       logmessage(NORMAL,"socket success");
       //cout<<"socket success"<<endl;
        struct sockaddr_in server;
        bzero(&server,sizeof(server));
        server.sin_family=AF_INET;
        server.sin_port=htons(_port);
        server.sin_addr.s_addr=inet_addr(_ip.c_str());
        int n=bind(_listensockfd,(struct sockaddr*)&server,sizeof(server));
        if(n<0)
        {
            logmessage(ERROR,"bind error");
         exit(1);
           
        }
        logmessage(NORMAL,"bind success");//监听
       cout<<"bind success"<<endl;

            if(listen(_listensockfd,gbacklog)<0)
            {
                logmessage(FATAL,"listen FATAL");
                exit(FATAL);
            }
         logmessage(NORMAL,"listen normal");

    }
    void run()
    {  //signal(SIGCHLD,SIG_IGN);
        Threadpool<Task>::getInstance()->run();
        while(true)
        {  
        struct sockaddr_in client;
        socklen_t len=sizeof(client);//必须进行初始化
        memset(&client,0,sizeof(client));
        int sock=accept(_listensockfd,(struct sockaddr *)&client,&len);
        if(sock<0)
        {
            logmessage(ERROR,"accept error");
            //cout<<"accept error"<<endl;
            continue;
        }
         logmessage(NORMAL,"accept success");
          //  cout<<"accept success"<<endl;
            //int n=fork();
            // if(n==0)
            // { close(_listensockfd);
            //   // if(fork()>0)
            //   // {
            //   //   exit(0);
            //   // }
            //   serviceIO(sock); //需要对它进行分多进程或多线程处理，因为要对多个客户端提高服务
            //   close(sock);
            // }
        Threadpool<Task>::getInstance()->push(Task(sock));

         }



    }
//  void serviceIO(int sock)//与类没有关系，可以放外面
//  {    
//     char buffer[2048];
//      while(true)
//      {
//       ssize_t n=read(sock,buffer,sizeof(buffer));
//       if(n>0)
//       {buffer[n]=0;
//       cout<<buffer<<endl;
//       string message;
//       message=buffer;
//       message+=" server[echo]";
//       write(sock,message.c_str(),message.size());
//       }
//       else
//       {
//       cout<<"读取失败"<<endl;
//         logmessage(ERROR,"read error");
//         break;
//       }
//      }
//  }


private:
  int _listensockfd;
  string _ip;
  uint16_t _port;

   
};